package com.mathdroid.gui.main.character;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;

import javax.swing.JPanel;

import com.mathdroid.model.Model;
import com.mathdroid.model.enums.EItemSlot;
import com.mathdroid.model.enums.EItemType;
import com.mathdroid.modules.ISpecProficiencies;

/**
 * This panel is the representation of the character model.
 */
public class CharacterPanel extends JPanel {
	/**
	 * Default UID.
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * The constraints of this panel.
	 */
	private final GridBagConstraints c = new GridBagConstraints();
	
	/**
	 * The number of item slot rows.
	 */
	private static final int ROWS = 7;
	
	/**
	 * The center area of this panel.
	 */
	private final CenterContent centerContent = new CenterContent();
	
	/**
	 * The amount of armor slots.
	 */
	private static final int ARMOR_SLOTS = 12;
	
	/**
	 * The armor slots. There are 12. 6 on the left and 6 on the right.
	 */
	private ItemSlot [] itemSlot = new ItemSlot[ARMOR_SLOTS];
	
	/**
	 * The left weapon slot.
	 */
	private ItemSlot mainhand = null;
	
	/**
	 * The right weapon slot.
	 */
	private ItemSlot offhand = null;
	
	/**
	 * The space between components.
	 */
	private static final int SPACING = 5;
	
	/**
	 * The weight of "heavy" components.
	 */
	private static final int HEAVY_WEIGHT = 10;
	
	/**
	 * Standard constructor.
	 */
	public CharacterPanel() {
		super();
		
		this.setLayout(new GridBagLayout());
		
		this.buildItemSlots();
		this.buildCenterContent();
	}
	
	/**
	 * Build the content in the middle.
	 */
	private void buildCenterContent() {
		this.c.fill = GridBagConstraints.BOTH;
		this.c.gridx = 1;
		this.c.gridy = 0;
		this.c.weightx = HEAVY_WEIGHT;
		this.c.weighty = HEAVY_WEIGHT;
		this.c.gridwidth = 2;
		this.c.gridheight = 6;
		this.c.insets = new Insets(SPACING, SPACING, SPACING, SPACING);
		this.add(this.centerContent, this.c);
	}

	/**
	 * Build the item slots on the left, right and bottom.
	 */
	private void buildItemSlots() {	
		this.c.gridwidth = 1;
		this.c.gridheight = 1;
		this.c.weightx = 1;
		this.c.weighty = 1;
		this.c.gridx = 0;
		this.c.insets = new Insets(SPACING, SPACING, SPACING, SPACING);
		this.c.fill = GridBagConstraints.BOTH;
		
		for (int i = 0; i < ARMOR_SLOTS; i++) {
			this.itemSlot[i] = 
				new ItemSlot(EItemSlot.getAllSlots().get(i), EItemType.ARMOR);
		}
		
		int k = 0;
		for (int y = 0; y < ROWS - 1; y++) {
			this.c.gridy = y;
			this.add(this.itemSlot[k], this.c);
			k++;
		}
		
		this.c.gridx = 3;
		for (int y = 0; y < ROWS - 1; y++) {
			this.c.gridy = y;
			this.add(this.itemSlot[k], this.c);
			k++;
		}

		ISpecProficiencies sp = Model.getESpec().getProficiencies();
		this.mainhand = new ItemSlot(sp.getWeaponSlots().get(0), 
				EItemType.WEAPON);
		this.offhand = new ItemSlot(sp.getWeaponSlots().get(1),
				EItemType.WEAPON);
		
		this.c.gridx = 1;
		this.c.gridy = ROWS - 1;
		this.add(this.mainhand, this.c);
		k++;
		
		this.c.gridx = 2;
		this.c.gridy = ROWS - 1;
		this.add(this.offhand, this.c);
	}
}
